home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
brent_cc.lha
/
brent_cc
/
vzeroin.cc
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-08
|
1KB
|
54 lines
// This may look like C code, but it is really -*- C++ -*-
//
// Verify ZEROIN routine
#include "math_num.h"
#include <ostream.h>
static int counter; // Iteration counter
// Run a test
static void test
(const double a, const double b, double (*f)(double x), const char *msg)
{
double root;
counter = 0;
cout << form("\nFor function %s,\nthe root over [%g,%g] is\t%.9e\n",msg,a,b,
(root=zeroin(a,b,f)) );
cout << form("Function value at the root found\t%.4e\n"
"No. of iterations\t\t%d\n",
(*f)(root), counter);
}
double f1(const double x) // Test from the Forsythe book
{
counter++;
return (sqr(x)-2)*x - 5;
}
double f2(const double x)
{
counter++;
return cos(x) - x;
}
double f3(const double x)
{
counter++;
return sin(x) - x;
}
main()
{
cout << "\n\n" << _Minuses <<
"\n\t\tTesting the Brent's root finder\n\n";
test(2.0,3.0,f1,"x^3 - 2*x - 5");
cout << "Exact root is \t\t2.0945514815\n";
test(2.0,3.0,f2,"cos(x)-x");
test(-1.0,3.0,f2,"cos(x)-x");
test(-1.0,3.0,f3,"sin(x)-x");
}